<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\console\command\optimize\Schema;
use think\Db;

/**
 * 流程
 */
class Liucheng extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 流程 大分类
     */
    public function index()
    {

        $list=Db::name('yunyin')->order('weigh desc')->select();

        $user=$this->auth->getUser();

        foreach ($list as &$key){

            $info=Db::name('see_log')->where(['user_id'=>$user['id'],'find'=>'yunyin'.$key['id']])->find();

            $key['look']=$info?'1':'';
        }


        $this->success('请求成功',$list);
    }

    /**
     * 项目流程
     */

    public function getlistbyid(){

        $user=$this->auth->getUser();
        $id=$this->request->param('id');

        $list=Db::name('yunyin_c')->where(['yunyin_id'=>$id])->order('weigh desc')->select();

        foreach ($list as &$key){

            $info=Db::name('see_log')->where(['user_id'=>$user['id'],'find'=>'yunyinc'.$key['id']])->find();

            $key['look']=$info?'1':'';
        }


        $this->success('项目流程',$list);

    }

    //全部查看

    public function allsuccess(){
        $user=$this->auth->getUser();
        $id=$this->request->param('id');

        $find=Db::name('yunyin_c')->where(['id'=>$id])->find();
        $cate=Db::name('yunyin')->where(['id'=>$find['yunyin_id']])->find();



        $logs['user_id']=$user['id'];
        $logs['find']='yunyin'.$cate['id'];
        $ind2=Db::name('see_log')->where($logs)->find();

        if ($ind2){
            $this->error('');
        }


            $logs['desc']=$user['nickname'].'完成了'.$find['name'];
            $logs['time']=time();

            Db::name('see_log')->data($logs)->insert();


        $list=Db::name('yunyin_c')->where(['yunyin_id'=>$cate['id']])->select();


        foreach ($list as $ke){

            $log['user_id']=$user['id'];
            $log['find']='yunyinc'.$ke['id'];
            $log['desc']=$user['nickname'].'完成了'.$ke['name'];
            $log['time']=time();
            $log['name']=$ke['yunyin_id'];
            Db::name('see_log')->data($log)->insert();

        }




        $this->success('完成！');

    }


    //单个查看
    public function asuccess(){

        $user=$this->auth->getUser();
        $id=$this->request->param('id');

        $find=Db::name('yunyin_c')->where(['id'=>$id])->find();


        $logs['user_id']=$user['id'];
        $logs['find']='yunyinc'.$find['id'];

        $ind2=Db::name('see_log')->where($logs)->find();


        if ($ind2){
            $this->error('ssss');
        }

        $log['name']=$find['yunyin_id'];
        $log['user_id']=$user['id'];
        $log['find']='yunyinc'.$find['id'];
        $log['desc']=$user['nickname'].'完成了'.$find['name'];
        $log['time']=time();

        Db::name('see_log')->data($log)->insert();


        $logg['name']=$find['yunyin_id'];
        $logg['user_id']=$user['id'];
        $count=Db::name('see_log')->where($logg)->count();

        $count_y=Db::name('yunyin_c')->where(['yunyin_id'=>$find['yunyin_id']])->count();

        //如果都学习了
        if ($count==$count_y){

            $fb=Db::name('yunyin')->where(['id'=>$find['yunyin_id']])->find();

            $logc['user_id']=$user['id'];
            $logc['find']='yunyin'.$fb['id'];
            $logc['desc']=$user['nickname'].'完成了'.$fb['name'];
            $logc['time']=time();

            Db::name('see_log')->data($logc)->insert();
        }


        $this->success('操作成功');

    }

    //详情
    public function getinfobyid(){

        $user=$this->auth->getUser();
        $id=$this->request->param('id');

        $find=Db::name('yunyin_c')->where(['id'=>$id])->find();


        $find['v_link']=cdnurl($find['file'],true);
        $this->success('操作成功',$find);


    }

}